package ch.unisi.inf.pfii.teamwhite.risk.core;

/**
 * 
 * @author Matteo Bellan: bellanm@lu.unisi.ch
 * @author Andrea Michelotti: micheloa@lu.unisi.ch
 * @author Ranganathan Ramasubramanian: ramasubr@lu.unisi.ch
 *
 */

import java.util.ArrayList;

import ch.unisi.inf.pfii.teamwhite.risk.core.io.MapReader;
import ch.unisi.inf.pfii.teamwhite.risk.core.player.Gaia;

//This class represents the entire battlefield which contains all the planets
public final class Galaxy {

	private ArrayList<Planet> planets;
	private final static int MONEY = 500;
	private MapReader mapReader;

//	This constructor creates the galaxy reading from a file which contains planets and neighbours
	public Galaxy(final String mapName) {
		planets = new ArrayList<Planet>();
		mapReader = new MapReader();
		for (int i = 10; i < 52; i++) {
			planets.add(new Planet("Planet" + i, new Gaia(), MONEY));
		}
		for (Planet planet : planets) {
			planet.setNeighbours(neighboursLister(mapReader.lister(planet.getName(), (mapName + ".txt"))));
			planet.getNeighbours().remove(planet);
			planet.setRegion(mapReader.lister(planet.getName(), (mapName + "Region.txt"))[1]);
		}
	}

	public final ArrayList<Planet> getPlanets() {
		return planets;
	}

//	This method search for a planet in the planets ArrayList
	private final Planet find(final String planetName) {
		Planet result = null;
		for (Planet planet : planets) {
			if (planetName.equals(planet.getName())) {
				result = planet;
			}
		}
		return result;
	}

//	This method, given an Array which contains all the neighbours of a planet, creates the link between the planets
	private final ArrayList<Planet> neighboursLister(final String[] neighbours) {
		ArrayList<Planet> result = new ArrayList<Planet>();
		for (String name : neighbours) {
			result.add(find(name));
		}
		return result;
	}
}
